package binary_tree.Heap;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Queue;
import java.util.PriorityQueue;

/**
 * @Description: 测试优先级队列
 * @Author Stark
 * @Date 2022/3/6 9:51
 **/
public class PriorityQueueTest {
    public static void main(String[] args) {
        //基本类型
//        int[] arr = new int[]{3,5,7,9,1,5,7,2,4};
//        Queue<Integer> queue1 = new PriorityQueue<>();
//        for(int tmp : arr){
//            queue1.offer(tmp);
//        }
//        int[] ret = new int[arr.length];
//        for (int i = 0; i < arr.length; i++) {
//            ret[i] = queue1.poll();
//        }
//        System.out.println(Arrays.toString(ret));

        //Queue<Student> queue = new PriorityQueue<>(new StudentCom());
        //Queue<Student> queue = new PriorityQueue<>(new StudentComDesc());
        //匿名构造
        Queue<Student> queue = new PriorityQueue<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        //函数式编程,Lambda表达式
        //Queue<Student> queue = new PriorityQueue<>();
//        Queue<Student> queue = new PriorityQueue<>((o1,o2) -> o1.getAge() - o2.getAge());
        Student stu1 = new Student("师傅",40);
        Student stu2 = new Student("哥哥",20);
        Student stu3 = new Student("姐姐",30);
        Student stu4 = new Student("老师",35);
        System.out.println("排序：");
        Student[] students = {stu1,stu2,stu3,stu4};
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));

        System.out.println("优先级队列：");
        queue.offer(stu1);
        queue.offer(stu2);
        queue.offer(stu3);
        queue.offer(stu4);
        System.out.println(queue);
    }
}
//使用Comparator比较器,实现无侵入编程
//实现小堆比较的类
class StudentCom implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}
//实现大堆比较的类
class StudentComDesc implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }
}

 class Student {
    private String name;
    private int age;

    public int getAge() {
        return age;
    }
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

     @Override
     public boolean equals(Object obj) {
        Student o = (Student) obj;
         return this.name.equals(o.name) && this.age == o.age;
     }

     @Override
     public String toString() {
         return "Student{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
     }

//     @Override
//     //实现Comparable接口实现自定义类型可比较
//     public int compareTo(Student o) {
//         return this.age - o.age;
//     }
 }

